package com.example.liquortest;

import okhttp3.Request;
import org.noear.liquor.eval.Scripts;
import org.noear.solon.Solon;
import org.noear.solon.annotation.SolonMain;

import java.util.HashMap;
import java.util.Map;

@SolonMain
public class App {
    static void main(String[] args) {
        Solon.start(App.class, args);
        Map<String,Object> context = new HashMap<>();
        Request.Builder requestBuilder = new Request.Builder();
        okhttp3.HttpUrl.Builder urlBuilder = okhttp3.HttpUrl.parse("https://192.168.17.15/artemis/api/resource/v2/acsDevice/search").newBuilder();
        context.put("requestBuilder", requestBuilder);
        context.put("urlBuilder", urlBuilder);
        var script = """
                import okhttp3.Headers;
                import okhttp3.Request;
                
                import java.nio.charset.StandardCharsets;
                import java.util.Date;
                import java.util.HashMap;
                import java.util.Map;
                import java.util.UUID;
                
                Map<String, String> headers = new HashMap<>();
                headers.put("x-ca-key","26606678");
                headers.put("x-ca-nonce",UUID.randomUUID().toString());
                headers.put("x-ca-timestamp",String.valueOf(new Date().getTime()));
                headers.put("x-ca-signature-headers","x-ca-key,x-ca-nonce,x-ca-timestamp");
                headers.forEach((k, v) -> {
                	if (v != null) {
                		headers.put(k, new String(v.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
                	}
                });
                requestBuilder.headers(Headers.of(headers));
                """;
        Scripts.eval(script, context);
    }
}